package sk.atletis.atletika;

import java.util.Calendar;
import java.util.Locale;

import android.app.ActionBar;
import android.app.AlarmManager;
import android.app.FragmentTransaction;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
	public static BroadcastReceiver receiver;
		
	SectionsPagerAdapter mSectionsPagerAdapter;
	ViewPager mViewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
         
		// Set up the action bar.
		final ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);
		
		// When swiping between different sections, select the corresponding
		// tab. We can also use ActionBar.Tab#select() to do this if we have
		// a reference to the Tab.
		mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() 
		{
			@Override
			public void onPageSelected(int position) 
			{
				actionBar.setSelectedNavigationItem(position);
			}
		});

		// For each of the sections in the app, add a tab to the action bar.
		for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) 
		{
			actionBar.addTab(actionBar.newTab()
					.setText(mSectionsPagerAdapter.getPageTitle(i))
					.setTabListener(this));
		}
		
		// Update data 
		updateData();
		
		// Setup updating
		setupDataUpdating();
	}

	@Override
	public void onResume() 
    {
		super.onResume();
		setLastViewedNow();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		super.onCreateOptionsMenu(menu);
		
		MenuItem info = menu.add(Menu.NONE, R.id.info, Menu.NONE, R.string.info);
		info.setIcon(R.drawable.ic_menu_info);
		info.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		
		MenuItem refresh = menu.add(Menu.NONE, R.id.refresh, Menu.NONE, R.string.refresh);
		refresh.setIcon(R.drawable.ic_menu_refresh);
		refresh.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		
        return true;
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) 
	{
        switch (item.getItemId()) 
        {
        	// Refresh data
        	case R.id.refresh:
        		updateData();
        		return true;
        		
        	case R.id.info:
        		// Intent detail = new Intent(this, HomeActivity.class);
        		Intent detail = new Intent(this, WebViewActivity.class);
				this.startActivity(detail);
        }

        return super.onOptionsItemSelected(item);
    }
	
	private void updateData()
	{
		Intent intent = new Intent(this, DataReceiver.class);
		sendBroadcast(intent);
	}
	
	@Override
	public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
	{
		mViewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) 
	{
		
	}

	@Override
	public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) 
	{
		
	}
	
	// Sets data updating
    private void setupDataUpdating() 
    {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 30);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.AM_PM, Calendar.AM);
		calendar.add(Calendar.DAY_OF_MONTH, 1);

		Intent intent = new Intent(this, DataReceiver.class);
		intent.putExtra(DataReceiver.ALARM_BROADCAST, true);
		
		PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);

		((AlarmManager) getSystemService(Context.ALARM_SERVICE)).setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_HALF_DAY, pi);
	}
    
    // Sets last viewed time
    private void setLastViewedNow() 
    {
		Intent intent = new Intent(ArticlesContract.ACTION_LASTVIEW_CHANGED);
		intent.putExtra(ArticlesContract.NEW_ARTICLES, 0);
    	sendBroadcast(intent);
	}

	// --- PAGER ---
	public class SectionsPagerAdapter extends FragmentPagerAdapter 
	{
		public SectionsPagerAdapter(FragmentManager fm) 
		{
			super(fm);
		}

		@Override
		public Fragment getItem(int position) 
		{
			Fragment fragment;
			
			switch(position)
			{
			case 0:
				fragment = new FragmentHomeNews();
				break;
				
			case 1:
				fragment = new FragmentWorldNews();
				break;
				
			case 2:
				fragment = new FragmentResults();
				break;
				
			default:
				fragment = new FragmentHomeNews();
				break;
			}
			
			Bundle args = new Bundle();
			args.putInt("SectionNumber", position + 1);
			fragment.setArguments(args);
			return fragment;
		}

		@Override
		public int getCount() 
		{
			// Show 2 total pages.
			return 3;
		}

		@Override
		public CharSequence getPageTitle(int position) 
		{
			Locale l = Locale.getDefault();
			
			switch (position) 
			{
			case 0:
				return getString(R.string.title_aktuality).toUpperCase(l);
			case 1:
				return getString(R.string.title_zo_sveta).toUpperCase(l);
			case 2:
				return getString(R.string.title_results).toUpperCase(l);
			}
			
			return null;
		}
	}
	
	
	

}
